package com.it.park.controller.expose;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.NewsCommentService;
import com.it.park.client.service.NewsService;
import com.it.park.common.JsonObj;
import com.it.park.constant.Constant;
import com.it.park.model.News;
import com.it.park.model.NewsComment;
import com.it.park.rms.client.model.User;

@Controller
@RequestMapping("/expose/news")
public class ExposeNewsController {
	
	private String rootPath=Constant.EXPOSE_PATH;

	@Autowired
	private NewsService newsService;
	@Autowired
	private NewsCommentService newsCommentService;
	
	/**
	 * 新闻图片页面
	 * @return
	 */
	@RequestMapping("/newsDetail")
	public String newsDetail(Integer id,HttpServletRequest request){
		News news = newsService.selectByPrimaryKey(id);
		news.setClickRate(news.getClickRate()+1);//浏览量加1
		newsService.updateByPrimaryKeySelective(news);
		request.setAttribute("news", news);
		return rootPath+"news/newsDetail";
	}
	
	/**
	 *新闻评论
	 */
	@RequestMapping("newsComment")
	@ResponseBody
	public JsonObj newsComment(NewsComment newsComment,HttpSession session){
		JsonObj json = new JsonObj();
		User user = (User) session.getAttribute("user");
		if(user != null){
			newsComment.setUserId(user.getId());
			NewsComment m = newsCommentService.selectIsExists(newsComment);
			if(m == null){
				boolean success =  newsCommentService.insertSelective(newsComment);
				if(success){//成功
					News news  = newsService.selectByPrimaryKey(newsComment.getNewsId());
					news.setCommentNum(news.getCommentNum()+1);
					newsService.updateByPrimaryKeySelective(news);
					json.setMessage(Constant.SUCCESS_COMMENT_MSG);
					json.setCode(Constant.SUCCESS100);
					json.setSuccess(Constant.S);
					json.setObject(news);
				}else{//失败
					json.setMessage(Constant.FAIL_COMMENT_MSG);
					json.setCode(Constant.FAIL101);
					json.setSuccess(Constant.F);
				}
			}else{//已经评论过
				json.setMessage(Constant.HAS_COMMENT_MSG);
				json.setCode(Constant.FAIL103);
				json.setSuccess(Constant.F);
			}
		}else{//未登录
			json.setMessage(Constant.NOT_LOGGED_MSG);
			json.setCode(Constant.NOT_LOGGED102);
			json.setSuccess(Constant.F);
		}
		return json;
	}
	
	/**
	 * 新闻评论
	 * @return
	 */
	@RequestMapping("newsCommentListByNewsId")
	@ResponseBody
	public List<NewsComment> newsCommentListByNewsId(HttpServletRequest request,Integer startPos,Integer newsId,HttpSession session){
			Map<String,Object> map =new HashMap<String,Object>();
			map.put("newsId", newsId);
			map.put("startPos",startPos != null?startPos:0);
			List<NewsComment>  comments = newsCommentService.selectByNewsId(map);
			return comments;
	}
	
}
